<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">


      <el-form-item label="" prop="orderStatus">
        <el-select v-model="queryParams.orderStatus" @change="handleQuery" placeholder="请选择订单状态" clearable size="small">
          <el-option
            v-for="dict in orderStatusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="" prop="orderSn">
        <el-input
          v-model="queryParams.orderSn"
          placeholder="请输入订单编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="" prop="consignee">
        <el-input
          v-model="queryParams.consignee"
          placeholder="请输入收货人名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="" prop="mobile">
        <el-input
          v-model="queryParams.mobile"
          placeholder="请输入收货人手机号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="" prop="address">
        <el-input
          v-model="queryParams.address"
          placeholder="请输入收货具体地址"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="" prop="payId">
        <el-input
          v-model="queryParams.payId"
          placeholder="请输入微信付款编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['shop:order:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['shop:order:export']"
        >导出</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="orderList" @selection-change="handleSelectionChange">
<!--      <el-table-column type="selection" width="55" align="center" />-->
      <el-table-column type="expand">
        <template slot-scope="props">
          <div v-for="item in props.row.goodsVoList" :key="item.id" class="order-goods">
            <div class="picture">
              <img :src="item.picUrl" width="40">
            </div>
            <div class="name">
              商品名称：{{ item.goodsName }}
            </div>
            <div class="spec">
              规格：{{ item.specifications.join('-') }}
            </div>
            <div class="price">
              单价：{{ item.price }} 元
            </div>
            <div class="num">
              数量：{{ item.number }} 件
            </div>
            <div class="price">
              小计：{{ item.price * item.number }} 元
            </div>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="订单编号" align="center" prop="orderSn" />
      <el-table-column align="center" property="iconUrl" label="用户头像" width="80">
        <template slot-scope="scope">
          <el-avatar :src="scope.row.memberPic" />
        </template>
      </el-table-column>
      <el-table-column label="下单用户" align="center" prop="memberNickname" width="80"/>
      <el-table-column label="下单时间" align="center" prop="addTime" width="180"/>
      <el-table-column label="订单状态" align="center" prop="orderStatus" :formatter="orderStatusFormat" width="80" />
      <el-table-column label="订单金额" align="center" prop="orderPrice" width="80"/>
      <el-table-column label="实付金额" align="center" prop="actualPrice" width="80" />
      <el-table-column label="支付时间" align="center" prop="payTime" width="180" />
      <el-table-column align="center" label="收货人" prop="consignee" width="80">
        <template slot-scope="scope">
          <span style="color:red; font-weight:bold;">{{ scope.row.consignee }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="物流单号" prop="shipSn" width="100"/>

<!--      <el-table-column align="center" label="物流渠道" prop="shipChannel" />
      <el-table-column align="center" label="收货电话" prop="mobile" min-width="100" />
      <el-table-column label="收货具体地址" align="center" prop="address" />
      <el-table-column label="用户订单留言" align="center" prop="message" />
      <el-table-column label="商品总费用" align="center" prop="goodsPrice" />
      <el-table-column label="配送费用" align="center" prop="freightPrice" />
      <el-table-column label="优惠券减免" align="center" prop="couponPrice" />
      <el-table-column label="用户积分减免" align="center" prop="integralPrice" />
      <el-table-column label="团购优惠价减免" align="center" prop="grouponPrice" />
      <el-table-column label="订单费用， = goods_price + freight_price - coupon_price" align="center" prop="orderPrice" />
      <el-table-column label="实付费用， = order_price - integral_price" align="center" prop="actualPrice" />
      <el-table-column label="微信付款编号" align="center" prop="payId" />
      <el-table-column label="微信付款时间" align="center" prop="payTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.payTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="发货编号" align="center" prop="shipSn" />
      <el-table-column label="发货快递公司" align="center" prop="shipChannel" />
      <el-table-column label="发货开始时间" align="center" prop="shipTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.shipTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="实际退款金额，" align="center" prop="refundAmount" />
      <el-table-column label="退款方式" align="center" prop="refundType" />
      <el-table-column label="退款备注" align="center" prop="refundContent" />
      <el-table-column label="退款时间" align="center" prop="refundTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.refundTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="用户确认收货时间" align="center" prop="confirmTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.confirmTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="待评价订单商品数量" align="center" prop="comments" />
      <el-table-column label="订单关闭时间" align="center" prop="endTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="addTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.addTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="逻辑删除" align="center" prop="deleted" />-->
      <el-table-column label="操作" align="center" class-name="oper">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" @click="handleDetail(scope.row)">详情</el-button>
          <el-button type="primary" size="mini" @click="handleShip(scope.row)" v-if="scope.row.orderHandleOption.canship">发货</el-button>
          <el-button type="warning" size="mini" @click="handlePay(scope.row)" v-if="scope.row.orderHandleOption.pay">收款</el-button>
          <el-button type="primary" size="mini" @click="viewShip(scope.row)" v-if="scope.row.orderHandleOption.viewship">物流</el-button>
          <el-button type="danger" size="mini" @click="handleRefund(scope.row)" v-if="scope.row.orderStatus == 202">退款</el-button>
          <el-button type="danger" size="mini" @click="handleDelete(scope.row)" v-if="scope.row.orderHandleOption.delete" v-hasPermi="['shop:order:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 订单详情对话框 -->
    <el-dialog :visible.sync="orderDialogVisible" title="订单详情" width="800">
      <section ref="print">
        <el-form :data="form" label-position="left">
          <el-form-item label="订单编号">
            <span>{{ form.orderSn }}</span>
          </el-form-item>
          <el-form-item label="订单状态">
            <el-tag>{{ orderStatusFormat(form) }}</el-tag>
          </el-form-item>
          <el-form-item label="订单用户">
            <span>{{ form.memberNickname }}</span>
          </el-form-item>
          <el-form-item label="用户留言">
            <span>{{ form.message }}</span>
          </el-form-item>
          <el-form-item label="收货信息">
            <span>（收货人）{{ form.consignee }}</span>
            <span>（手机号）{{ form.mobile }}</span>
            <span>（地址）{{ form.address }}</span>
          </el-form-item>
          <el-form-item label="商品信息">
            <el-table :data="form.goodsVoList" border fit highlight-current-row>
              <el-table-column align="center" label="商品名称" prop="goodsName" />
              <el-table-column align="center" label="商品编号" prop="goodsSn" />
              <el-table-column align="center" label="货品规格" prop="specifications" />
              <el-table-column align="center" label="货品价格" prop="price" />
              <el-table-column align="center" label="货品数量" prop="number" />
              <el-table-column align="center" label="货品图片" prop="picUrl">
                <template slot-scope="scope">
                  <img :src="scope.row.picUrl" width="40">
                </template>
              </el-table-column>
            </el-table>
          </el-form-item>
          <el-form-item label="费用信息">
            <span>
              (实际费用){{ form.actualPrice }}元 =
              (商品总价){{ form.goodsPrice }}元 +
              (快递费用){{ form.freightPrice }}元 -
              (优惠减免){{ form.couponPrice }}元 -
              (积分减免){{ form.integralPrice }}元
            </span>
          </el-form-item>
          <el-form-item label="支付信息">
            <span>（支付渠道）微信支付</span>
            <span>（支付时间）{{ form.payTime }}</span>
          </el-form-item>
          <el-form-item label="快递信息">
            <span>（快递公司）{{ form.shipChannel }}</span>
            <span>（快递单号）{{ form.shipSn }}</span>
            <span>（发货时间）{{ form.shipTime }}</span>
          </el-form-item>
          <el-form-item label="退款信息">
            <span>（退款金额）{{ form.refundAmount }}元</span>
            <span>（退款类型）{{ form.refundType }}</span>
            <span>（退款备注）{{ form.refundContent }}</span>
            <span>（退款时间）{{ form.refundTime }}</span>
          </el-form-item>
          <el-form-item label="收货信息">
            <span>（确认收货时间）{{ form.confirmTime }}</span>
          </el-form-item>
        </el-form>
      </section>
      <span slot="footer" class="dialog-footer">
        <el-button @click="orderDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="printOrder">打 印</el-button>
      </span>
    </el-dialog>

    <!-- 发货对话框 -->
    <el-dialog :visible.sync="shipDialogVisible" title="发货">
      <el-form ref="shipForm" :model="form" status-icon label-position="left" label-width="100px" style="width: 400px; margin-left:50px;">
        <el-form-item label="快递公司" prop="shipChannel">
          <el-select v-model="form.shipChannel" placeholder="请选择">
            <el-option v-for="item in channels" :key="item.code" :label="item.name" :value="item.code" />
          </el-select>
        </el-form-item>
        <el-form-item label="快递编号" prop="shipSn">
          <el-input v-model="form.shipSn" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="shipDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="confirmShip">确定</el-button>
      </div>
    </el-dialog>
    <!-- 查看物流对话框 -->
    <el-dialog :visible.sync="viewShipDialogVisible" title="查看物流">
      <el-form :model="form" status-icon label-position="left" label-width="100px" style="width: 400px; margin-left:50px;">
        <el-form-item label="快递公司" prop="shipChannel">
          <el-select v-model="form.shipChannel" placeholder="请选择">
            <el-option v-for="item in channels" :key="item.code" :label="item.name" :value="item.code" :disabled="true"/>
          </el-select>
        </el-form-item>
        <el-form-item label="快递编号" prop="shipSn">
          <el-input v-model="form.shipSn" :disabled="true" />
        </el-form-item>
        <el-form-item label="物流信息" prop="shipSn">
          <div class="block">
            <el-timeline :reverse="true">
              <el-timeline-item
                v-for="(activity, index) in expressList"
                :key="index"
                :timestamp="activity.AcceptTime">
                {{activity.AcceptStation}}
              </el-timeline-item>
            </el-timeline>
          </div>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="viewShipDialogVisible = false">关闭</el-button>
      </div>
    </el-dialog>

    <!-- 收款对话框 -->
    <el-dialog :visible.sync="payDialogVisible" title="订单收款" width="40%" center>
      <el-form ref="payForm" :model="form" status-icon label-position="left" label-width="100px">
        <div style="margin-bottom: 10px;">
          确认当前订单（订单编号 {{ form.orderSn }} ) 已经完成线下收款  ？
        </div>
        <el-form-item label="订单金额" prop="orderPrice">
          <el-input-number v-model="form.orderPrice" :controls="false" disabled />
        </el-form-item>
        <el-form-item label="付款金额" prop="actualPrice">
          <el-input-number v-model="form.actualPrice" :controls="false" />
        </el-form-item>
      </el-form>
      <el-table :data="form.goodsVoList">
        <el-table-column property="goodsName" label="商品" />
        <el-table-column label="规格">
          <template slot-scope="scope">
            {{ scope.row.specifications.join('-') }}
          </template>
        </el-table-column>
        <el-table-column property="number" width="100" label="下单数量" />
        <!-- <el-table-column label="实际数量" width="100">
          <template slot-scope="scope">
            <el-input-number v-model="scope.row.number" :min="0" :controls="false" />
          </template>
        </el-table-column> -->
      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button @click="payDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="confirmPay">确定</el-button>
      </div>
    </el-dialog>

    <!-- 退款对话框 -->
    <el-dialog :visible.sync="refundDialogVisible" title="退款">
      <el-form ref="refundForm" :model="form" status-icon label-position="left" label-width="100px" style="width: 400px; margin-left:50px;">
        <el-form-item label="订单金额" prop="orderPrice">
          {{form.orderPrice}}
        </el-form-item>
        <el-form-item label="实付金额" prop="actualPrice">
          {{form.actualPrice}}
        </el-form-item>
        <el-form-item label="退款方式" prop="refundType">
          <el-select v-model="form.refundType" placeholder="请选择退款方式" @change="changeRefundType">
            <el-option
              v-for="dict in orderRefundTypeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="parseInt(dict.dictValue)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="退款金额" prop="refundAmount">
          <el-input v-model="form.refundAmount" :disabled="refundAmountInputVisible" />
        </el-form-item>
        <el-form-item label="退款备注" prop="refundContent">
          <el-input v-model="form.refundContent" type="textarea" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="refundDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="confirmRefund">确定</el-button>
      </div>
    </el-dialog>

    <!-- 添加或修改订单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="用户表的用户ID" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入用户表的用户ID" />
        </el-form-item>
        <el-form-item label="订单编号" prop="orderSn">
          <el-input v-model="form.orderSn" placeholder="请输入订单编号" />
        </el-form-item>
        <el-form-item label="订单状态">
          <el-radio-group v-model="form.orderStatus">
            <el-radio label="1">请选择字典生成</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="收货人名称" prop="consignee">
          <el-input v-model="form.consignee" placeholder="请输入收货人名称" />
        </el-form-item>
        <el-form-item label="收货人手机号" prop="mobile">
          <el-input v-model="form.mobile" placeholder="请输入收货人手机号" />
        </el-form-item>
        <el-form-item label="收货具体地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入收货具体地址" />
        </el-form-item>
        <el-form-item label="用户订单留言" prop="message">
          <el-input v-model="form.message" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="商品总费用" prop="goodsPrice">
          <el-input v-model="form.goodsPrice" placeholder="请输入商品总费用" />
        </el-form-item>
        <el-form-item label="配送费用" prop="freightPrice">
          <el-input v-model="form.freightPrice" placeholder="请输入配送费用" />
        </el-form-item>
        <el-form-item label="优惠券减免" prop="couponPrice">
          <el-input v-model="form.couponPrice" placeholder="请输入优惠券减免" />
        </el-form-item>
        <el-form-item label="用户积分减免" prop="integralPrice">
          <el-input v-model="form.integralPrice" placeholder="请输入用户积分减免" />
        </el-form-item>
        <el-form-item label="团购优惠价减免" prop="grouponPrice">
          <el-input v-model="form.grouponPrice" placeholder="请输入团购优惠价减免" />
        </el-form-item>
        <el-form-item label="订单费用， = goods_price + freight_price - coupon_price" prop="orderPrice">
          <el-input v-model="form.orderPrice" placeholder="请输入订单费用， = goods_price + freight_price - coupon_price" />
        </el-form-item>
        <el-form-item label="实付费用， = order_price - integral_price" prop="actualPrice">
          <el-input v-model="form.actualPrice" placeholder="请输入实付费用， = order_price - integral_price" />
        </el-form-item>
        <el-form-item label="微信付款编号" prop="payId">
          <el-input v-model="form.payId" placeholder="请输入微信付款编号" />
        </el-form-item>
        <el-form-item label="微信付款时间" prop="payTime">
          <el-date-picker clearable size="small" style="width: 200px"
            v-model="form.payTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择微信付款时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="发货编号" prop="shipSn">
          <el-input v-model="form.shipSn" placeholder="请输入发货编号" />
        </el-form-item>
        <el-form-item label="发货快递公司" prop="shipChannel">
          <el-input v-model="form.shipChannel" placeholder="请输入发货快递公司" />
        </el-form-item>
        <el-form-item label="发货开始时间" prop="shipTime">
          <el-date-picker clearable size="small" style="width: 200px"
            v-model="form.shipTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择发货开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="实际退款金额，" prop="refundAmount">
          <el-input v-model="form.refundAmount" placeholder="请输入实际退款金额，" />
        </el-form-item>
        <el-form-item label="退款方式">
          <el-select v-model="form.refundType" placeholder="请选择退款方式">
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item>
        <el-form-item label="退款备注" prop="refundContent">
          <el-input v-model="form.refundContent" placeholder="请输入退款备注" />
        </el-form-item>
        <el-form-item label="退款时间" prop="refundTime">
          <el-date-picker clearable size="small" style="width: 200px"
            v-model="form.refundTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择退款时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="用户确认收货时间" prop="confirmTime">
          <el-date-picker clearable size="small" style="width: 200px"
            v-model="form.confirmTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择用户确认收货时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="待评价订单商品数量" prop="comments">
          <el-input v-model="form.comments" placeholder="请输入待评价订单商品数量" />
        </el-form-item>
        <el-form-item label="订单关闭时间" prop="closeTime">
          <el-date-picker clearable size="small" style="width: 200px"
            v-model="form.closeTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择订单关闭时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="创建时间" prop="addTime">
          <el-date-picker clearable size="small" style="width: 200px"
            v-model="form.addTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择创建时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="逻辑删除" prop="deleted">
          <el-input v-model="form.deleted" placeholder="请输入逻辑删除" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>




  </div>
</template>

<style lang="scss" scoped>
.el-table--medium th, .el-table--medium td {
  padding: 3px 0;
}

.el-input-number--medium {
  width: 100%;
}

.oper .el-button--mini {
  padding: 7px 4px;
  width: 40px;
  font-size: 10px;
  margin-left: 1px;
}

::v-deep .el-table__expanded-cell {
  padding: 6px 80px;
}

.order-goods {
  display: flex;
  justify-content: space-around;
  justify-items: center;
  align-items:center;
  padding: 6px 0;
}

.name {
  width: 400px;
}

.spec {
  width: 180px;
}

.price {
  width: 120px;
}

.num {
  width: 120px;
}
</style>
<script>
import { listOrder, getOrder, delOrder, addOrder, updateOrder, exportOrder ,listChannel,shipOrder,payOrder,refundOrder,getOrderExpress} from "@/api/shop/order";

export default {
  name: "Order",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 订单表格数据
      orderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 订单状态字典
      orderStatusOptions: [],
      // 订单退款方式字典
      orderRefundTypeOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: undefined,
        orderSn: undefined,
        orderStatus: undefined,
        consignee: undefined,
        mobile: undefined,
        address: undefined,
        message: undefined,
        goodsPrice: undefined,
        freightPrice: undefined,
        couponPrice: undefined,
        integralPrice: undefined,
        grouponPrice: undefined,
        orderPrice: undefined,
        actualPrice: undefined,
        payId: undefined,
        payTime: undefined,
        shipSn: undefined,
        shipChannel: undefined,
        shipTime: undefined,
        refundAmount: undefined,
        refundType: undefined,
        refundContent: undefined,
        refundTime: undefined,
        confirmTime: undefined,
        comments: undefined,
        closeTime: undefined,
        addTime: undefined,
        deleted: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        userId: [
          { required: true, message: "用户表的用户ID不能为空", trigger: "blur" }
        ],
        orderSn: [
          { required: true, message: "订单编号不能为空", trigger: "blur" }
        ],
        orderStatus: [
          { required: true, message: "订单状态不能为空", trigger: "blur" }
        ],
        consignee: [
          { required: true, message: "收货人名称不能为空", trigger: "blur" }
        ],
        mobile: [
          { required: true, message: "收货人手机号不能为空", trigger: "blur" }
        ],
        address: [
          { required: true, message: "收货具体地址不能为空", trigger: "blur" }
        ],
        message: [
          { required: true, message: "用户订单留言不能为空", trigger: "blur" }
        ],
        goodsPrice: [
          { required: true, message: "商品总费用不能为空", trigger: "blur" }
        ],
        freightPrice: [
          { required: true, message: "配送费用不能为空", trigger: "blur" }
        ],
        couponPrice: [
          { required: true, message: "优惠券减免不能为空", trigger: "blur" }
        ],
        integralPrice: [
          { required: true, message: "用户积分减免不能为空", trigger: "blur" }
        ],
        grouponPrice: [
          { required: true, message: "团购优惠价减免不能为空", trigger: "blur" }
        ],
        orderPrice: [
          { required: true, message: "订单费用， = goods_price + freight_price - coupon_price不能为空", trigger: "blur" }
        ],
        actualPrice: [
          { required: true, message: "实付费用， = order_price - integral_price不能为空", trigger: "blur" }
        ],
      },

      //查看订单详情
      orderDialogVisible : false,
      //查看物流对话框
      viewShipDialogVisible : false,
      //物流信息列表
      expressList: [],

      //发货对话框
      shipDialogVisible : false,
      channels: [],
      //发货渠道
      //收款对话框
      payDialogVisible : false,
      //退款对话框
      refundDialogVisible : false,
      refundAmountInputVisible : true,
    };
  },
  created() {
    this.getList();
    this.getChannel();
    //订单状态
    this.getDicts("biz_shop_order_status").then(response => {
      this.orderStatusOptions = response.data;
    });
    //订单退款方式
    this.getDicts("biz_shop_order_refund_type").then(response => {
      this.orderRefundTypeOptions = response.data;
    });
  },
  methods: {
    /** 查询订单列表 */
    getList() {
      this.loading = true;
      listOrder(this.queryParams).then(response => {
        this.orderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    //获取发货渠道
    getChannel() {
      listChannel().then(response => {
        this.channels = response.data
      })
    },
    // 订单状态字典翻译
    orderStatusFormat(row, column) {
      return this.selectDictLabel(this.orderStatusOptions, row.orderStatus);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        userId: undefined,
        orderSn: undefined,
        orderStatus: "0",
        consignee: undefined,
        mobile: undefined,
        address: undefined,
        message: undefined,
        goodsPrice: undefined,
        freightPrice: undefined,
        couponPrice: undefined,
        integralPrice: undefined,
        grouponPrice: undefined,
        orderPrice: undefined,
        actualPrice: undefined,
        payId: undefined,
        payTime: undefined,
        shipSn: undefined,
        shipChannel: undefined,
        shipTime: undefined,
        refundAmount: undefined,
        refundType: undefined,
        refundContent: undefined,
        refundTime: undefined,
        confirmTime: undefined,
        comments: undefined,
        closeTime: undefined,
        addTime: undefined,
        updateTime: undefined,
        deleted: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /**订单详情*/
    handleDetail(row) {
      getOrder(row.id).then(response => {
        this.form = response.data;
      });
      this.orderDialogVisible = true
    },
    /**订单打印*/
    printOrder() {
      this.$print(this.$refs.print)
      this.orderDialogVisible = false
    },
    /** 订单发货 */
    handleShip(row) {
      getOrder(row.id).then(response => {
        this.form = response.data;
      });
      this.shipDialogVisible = true
    },
    /** 订单发货--提交 */
    confirmShip() {
      this.$refs['shipForm'].validate((valid) => {
        if (valid) {
          shipOrder(this.form).then(response => {
            this.shipDialogVisible = false;
            this.msgSuccess("删除成功");
            this.getList();
          }).catch(response => {
            this.$notify.error({
              title: '失败',
              message: response.data.errmsg
            })
          })
        }
      })
    },
    /** 订单收款 */
    handlePay(row) {
      this.form = row;
      // this.payForm.orderId = row.id
      // this.payForm.orderSn = row.orderSn
      // this.payForm.oldMoney = row.actualPrice
      // this.payForm.newMoney = row.actualPrice
      // this.payForm.goodsList = row.goodsVoList
      // this.payForm.goodsList.forEach(element => {
      //   element.onumber = element.number
      // })
      this.payDialogVisible = true
    },
    /** 订单收款 提交*/
    confirmPay() {
      if (this.form.oldMoney !== this.form.newMoney) {
        const diff = this.form.orderPrice - this.form.actualPrice
        this.$confirm('差额 ' + diff + '元， 是否确认提交')
          .then(_ => {
            this.confirmPay2()
          })
          .catch(_ => {})
      } else {
        this.confirmPay2()
      }
    },
    confirmPay2() {
      payOrder(this.form).then(response => {
        this.$notify.success({
          title: '成功',
          message: '订单收款操作成功'
        })
        this.getList()
      }).catch(response => {
        this.$notify.error({
          title: '失败',
          message: response.data.errmsg
        })
      }).finally(() => {
        this.payDialogVisible = false
      })
    },
    /** 查看物流 */
    viewShip(row) {
      this.form = row;
      this.expressList = [];
        getOrderExpress(row).then(response => {
        this.expressList = response.data.Traces;
      });
      this.viewShipDialogVisible = true
    },



    /** 订单退款 */
    handleRefund(row) {
      this.form = row;
      this.form.refundAmount = row.actualPrice
      this.refundDialogVisible = true
      this.$nextTick(() => {
        this.$refs['refundForm'].clearValidate()
      })
    },
    /** 订单退款--修改退款方式 交互响应调整 */
    changeRefundType(value) {
      if (2 == value) {
        // 线下打款: 退款金额可修改
        this.refundAmountInputVisible = false;
      } else {
        // 原路退回 退款金额不能改
        this.form.refundAmount = this.form.actualPrice;
        this.refundAmountInputVisible = true;
      }
    },
    /** 订单退款 提交*/
    confirmRefund() {
      this.$refs['refundForm'].validate((valid) => {
        if (valid) {
          refundOrder(this.form).then(response => {
            this.refundDialogVisible = false
            this.refundAmountInputVisible = true;
            this.$notify.success({
              title: '成功',
              message: '确认退款成功'
            })
            this.getList();
          }).catch(response => {
            this.$notify.error({
              title: '失败',
              message: response.data.errmsg
            })
          })
        }
      })
    },


    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加订单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getOrder(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改订单";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateOrder(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          } else {
            addOrder(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除订单编号为"' + row.orderSn + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delOrder(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有订单数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportOrder(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    }
  }
};
</script>
